package com.zx.dao.impl;

import com.zx.bean.Enterprise;
import com.zx.bean.Express;
import com.zx.dao.EnterpriseDao;
import com.zx.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EnterpriseDaoImpl implements EnterpriseDao {
    private  static  final String  SQL_FIND_LIMIT="select * from enterprise limit ?,?";  //分页查询全部
    private static final String SQL_FIND_ALL = "select * from enterprise";
    private static final String SQL_INSERT="insert into enterprise (name,profit,mobility,payOff,greenRating) values(?,?,?,?,?)";
    private  static  final String  SQL_CONSOLE="select count(id) data1_size,count(to_days(intime)=to_days(now()) and status=1 or null) data1_day," +
            "count(status=0 or null) data2_size,count(to_days(intime)=to_days(now()) and status=0 or null) data2_day " +
            "from express;";
    @Override
    public boolean insert(Enterprise enterprise) {
        //1 连接的获取
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement =null;

        try {
            //2预编译SQL
            preparedStatement=connection.prepareStatement(SQL_INSERT);
            //3 填充参数
            preparedStatement.setString(1,enterprise.getName());
            preparedStatement.setDouble(2,enterprise.getProfit());
            preparedStatement.setDouble(3,enterprise.getMobility());
            preparedStatement.setDouble(4,enterprise.getPayOff());
            preparedStatement.setInt(5,enterprise.getGreenRating());
            //4 执行SQL获取执行结果
            return  preparedStatement.executeUpdate()>0?true:false;
            //6 释放资源
        } catch (SQLException e1) {
            System.out.println(e1.getStackTrace());
        } finally {
            DruidUtil.close(connection,preparedStatement, null);
        }

        return false;

    }

    @Override
    public List<Enterprise> findAll(boolean limit, int offset, int pageNumber) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        List<Enterprise> data=new ArrayList<>();

        try {
            //2预编译SQL
            if(limit){
                preparedStatement = connection.prepareStatement(SQL_FIND_LIMIT);
                //3 填充参数
                preparedStatement.setInt(1,offset);
                preparedStatement.setInt(2,pageNumber);

            }else{
                preparedStatement=connection.prepareStatement(SQL_FIND_ALL);

            }


            //4 执行SQL
            resultSet=preparedStatement.executeQuery();
            //5 获取执行结果
            while(resultSet.next()){
                String name=resultSet.getString("name");
                double profit=resultSet.getDouble("profit");
                double mobility=resultSet.getDouble("mobility");
                double payOff=resultSet.getDouble("payOff");
                int greenRating=resultSet.getInt("greenRating");
//                String username=resultSet.getString("username");
//                String userphone=resultSet.getString("userphone");
//                String company=resultSet.getString("company");
//                String code=resultSet.getString("code");
//                Timestamp intime=resultSet.getTimestamp("intime");
//                Timestamp outtime=resultSet.getTimestamp("outtime");
//                int status=resultSet.getInt("status");
//                String sysPhone=resultSet.getString("sysPhone");
                Enterprise enterprise=new Enterprise();
                enterprise.setName(name);
                enterprise.setProfit(profit);
                enterprise.setMobility(mobility);
                enterprise.setPayOff(payOff);
                enterprise.setGreenRating(greenRating);
                data.add(enterprise);
            }
            //6 释放资源
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement, resultSet);
        }
        return data;
    }
}
